package com.cloudcc.mobile.view.web;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dialog.CallLogLoadingDialog;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.util.DialogUtils;
import com.cloudcc.mobile.util.DialogUtils.OnDoClickListener;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.base.BaseWebViewFragment;
import com.cloudcc.mobile.weight.CustomProgressDialog;

public class WebFunFragment extends BaseWebViewFragment {

	private final int OPEN_RESULT = 1;
	private final int PINK_RESULT = 2;
	private ValueCallback<Uri> mUploadMessage;
	private String obj = StringUtils.EMPTY;

	@Override
	public void initWebView() {

		Bundle bundle = getArguments();

		obj = bundle.getString("obj");
		mUrl = bundle.getString(IWebView.KEY_URL);
		LogUtils.d("WebView:", "url:" + mUrl);
//		progressDialog = new CallLogLoadingDialog(getActivity(), R.style.DialogLoadingTheme);
		setClient();
		super.initWebView();
	}

	private void setClient() {
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				startProgressDialog();
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				stopProgressDialog();
				super.onPageFinished(view, url);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url.equals(UrlManager.APP_UR + UrlTools.getFanhuiUrl(obj))
						|| url.equals(UrlManager.APP_UR + UrlTools.FanhuiTask)
						|| url.equals(UrlManager.APP_UR + UrlTools.FanhuiEvent)) {
					getActivity().finish();
					return true;
				}
				if (UrlManager.isHome(url)) {
					getActivity().finish();
					return true;
				}
				view.loadUrl(url);
				return true;
			}

			public void onReceivedSslError(WebView view,
					android.webkit.SslErrorHandler handler,
					android.net.http.SslError error) {
				handler.proceed();
			};

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				AppContext.isError = true;
				super.onReceivedError(view, errorCode, description, failingUrl);
			}
		});
		mWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					final JsResult result) {
				return true;
			}

			@Override
			public boolean onJsConfirm(WebView view, String url,
					String message, final JsResult result) {
				DialogUtils dialogutils = new DialogUtils(mContext);
				dialogutils.showUpdateDialog(mContext, "网页提示", message);
				dialogutils.setOnDoClicktListener(new OnDoClickListener() {
					@Override
					public void confirm() {
						result.confirm();
					}

					@Override
					public void cancel() {
						result.cancel();
					}
				});
				return true;
			}

			public void openFileChooser(ValueCallback<Uri> uploadMsg,
					String acceptType) {
				if (mUploadMessage != null)
					return;
				mUploadMessage = uploadMsg;
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.addCategory(Intent.CATEGORY_OPENABLE);
				i.setType("*/*");
				startActivityForResult(Intent.createChooser(i, "选择文件"),
						PINK_RESULT);
			}

			// For Android > 4.1.1
			public void openFileChooser(ValueCallback<Uri> uploadMsg,
					String acceptType, String capture) {
				openFileChooser(uploadMsg, acceptType);
			}

			// For Android < 3.0
			public void openFileChooser(ValueCallback<Uri> uploadMsg) {
				openFileChooser(uploadMsg, "");
			}
		});
	}

	@Override
	protected String getUrl() {
		return mUrl;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == PINK_RESULT) {
			if (null == mUploadMessage)
				return;
			Uri result = intent == null
					|| resultCode != getActivity().RESULT_OK ? null : intent
					.getData();
			mUploadMessage.onReceiveValue(result);
			mUploadMessage = null;
		}
	}

}
